﻿using System;
using System.Web.Mvc;
using DTD.Repositories;
using DTD.Repositories.Entities;
using DTD.Repositories.Interfaces;

namespace DTD.Web.Controllers
{
    public class TasksController : Controller
    {
        ITaskRepository repository = new TaskRepository();

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Add(Task task)
        {
            try
            {
                task.CreateTime = DateTime.Now;
                task.Status = (int)TaskStatuses.NotReady;
                repository.Add(task);
            }
            catch (Exception exception)
            {
                return this.Json(new {Success = false, Data = exception.Message});
            }
            return this.Json(new { Success = true });
        }

        public ActionResult Edit(Task task, string category)
        {
            try
            {
                repository.Update(task, category);
            }
            catch(Exception exception)
            {
                return Json(new {Success = false});
            }
            return Json(new {Success = true});
        }

        public ActionResult MarkDone(int Id)
        {
            repository.MarkAsDone(Id);
            return Json(new {Success = true});
        }
    }
}
